<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="唐奇杭"><meta name="copyright" content="唐奇杭"><meta name="generator" content="Hexo 5.3.0"><meta name="theme" content="hexo-theme-yun"><title>tcp模拟http请求以及响应处理 | 奇坑的小网站</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.22/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_ed8vp4atwoj.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/blog/yun.svg"><link rel="mask-icon" href="/blog/yun.svg" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/blog/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/blog/js/utils.js" as="script"><link rel="preload" href="/blog/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/blog/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/blog/css/hexo-theme-yun.css"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"tangqihang.gitee.io","root":"/blog/","title":"奇坑的小网站","version":"1.3.0","mode":"auto","copycode":true,"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/blog/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><meta name="description" content="node的net模块模拟http请求以及响应处理">
<meta property="og:type" content="article">
<meta property="og:title" content="tcp模拟http请求以及响应处理">
<meta property="og:url" content="https://tangqihang.gitee.io/blog/2021/01/27/node/tcp%E6%A8%A1%E6%8B%9Fhttp%E8%AF%B7%E6%B1%82%E4%BB%A5%E5%8F%8A%E5%93%8D%E5%BA%94%E5%A4%84%E7%90%86/index.html">
<meta property="og:site_name" content="奇坑的小网站">
<meta property="og:description" content="node的net模块模拟http请求以及响应处理">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://ftp.bmp.ovh/imgs/2021/01/4946990e34cae97e.jpg">
<meta property="article:published_time" content="2021-01-27T14:41:50.000Z">
<meta property="article:modified_time" content="2021-01-27T15:06:59.448Z">
<meta property="article:author" content="唐奇杭">
<meta property="article:tag" content="node">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ftp.bmp.ovh/imgs/2021/01/4946990e34cae97e.jpg"><script src="/blog/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/blog/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/blog/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button sidebar-nav-active" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel sidebar-panel-active" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/blog/about/" title="唐奇杭"><img width="96" loading="lazy" src="/blog/22.png" alt="唐奇杭"></a><div class="site-author-name"><a href="/blog/about/">唐奇杭</a></div><a class="site-name" href="/blog/about/site.html">奇坑的小网站</a><sub class="site-subtitle">西郊有密林，助君出重围</sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/blog/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/blog/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">23</span></a></div><div class="site-state-item"><a href="/blog/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><div class="site-state-item"><a href="/blog/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://github.com/summer-boythink" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=1754803981" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/277650245" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/blog/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content">This post does not have a Table of Contents</div></div></div><div class="tag-cloud"><div class="tag-cloud-tags"><a href="/blog/tags/GO/" style="font-size: 25.5px; color: #2680d4">GO</a> <a href="/blog/tags/JS/" style="font-size: 21px; color: #4d89c0">JS</a> <a href="/blog/tags/Python/" style="font-size: 12px; color: #999">Python</a> <a href="/blog/tags/Socket/" style="font-size: 12px; color: #999">Socket</a> <a href="/blog/tags/Vue/" style="font-size: 16.5px; color: #7391ad">Vue</a> <a href="/blog/tags/Zinx/" style="font-size: 16.5px; color: #7391ad">Zinx</a> <a href="/blog/tags/node/" style="font-size: 21px; color: #4d89c0">node</a> <a href="/blog/tags/%E4%BA%8C%E5%8F%89%E6%A0%91/" style="font-size: 12px; color: #999">二叉树</a> <a href="/blog/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 30px; color: #0078e7">随笔</a></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="https://tangqihang.gitee.io/blog/blog/2021/01/27/node/tcp%E6%A8%A1%E6%8B%9Fhttp%E8%AF%B7%E6%B1%82%E4%BB%A5%E5%8F%8A%E5%93%8D%E5%BA%94%E5%A4%84%E7%90%86/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="唐奇杭"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="奇坑的小网站"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">tcp模拟http请求以及响应处理</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2021-01-27 22:41:50" itemprop="dateCreated datePublished" datetime="2021-01-27T22:41:50+08:00">2021-01-27</time></div><div class="post-classify"><span class="post-category"> <span class="post-meta-item-icon" style="margin-right:3px;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span><span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/blog/categories/node/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">node</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/blog/tags/node/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">node</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><a id="more"></a>
<ol>
<li>模拟http请求</li>
</ol>
<ul>
<li>其实 http无非就是一个 规范更加严格的tcp。他是一个一次性的，并且要求tcp对他说<code>GET /?aa=11 HTTP/1.1\r\n</code>之类的话，这样另一变的http服务器才会鸟他，所以下面用一个类来封装一下 tcp要对http说的话<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">Request</span></span>&#123;</span><br><span class="line">    <span class="comment">//这个主要为了类等会用起来的时候会传来一些参数</span></span><br><span class="line">    <span class="comment">//把这些参数简单处理下</span></span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">option</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.method = option.method || <span class="string">&quot;GET&quot;</span>;</span><br><span class="line">        <span class="built_in">this</span>.host = option.host</span><br><span class="line">        <span class="built_in">this</span>.path = option.path || <span class="string">&quot;/&quot;</span>;</span><br><span class="line">        <span class="built_in">this</span>.port = option.port || <span class="number">80</span>;</span><br><span class="line">        <span class="built_in">this</span>.body = option.body || &#123;&#125;;</span><br><span class="line">        <span class="built_in">this</span>.headers = option.headers || &#123;&#125;;</span><br><span class="line">        <span class="keyword">if</span>(!<span class="built_in">this</span>.headers[<span class="string">&#x27;Content-Type&#x27;</span>])&#123;</span><br><span class="line">            <span class="built_in">this</span>.headers[<span class="string">&#x27;Content-Type&#x27;</span>] = <span class="string">&quot;application/x-www-from-urlencoded&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">this</span>.headers[<span class="string">&#x27;Content-Type&#x27;</span>] == <span class="string">&quot;application/json&quot;</span>)&#123;</span><br><span class="line">            <span class="built_in">this</span>.bodyText = <span class="built_in">JSON</span>.stringify(<span class="built_in">this</span>.body)</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">this</span>.headers[<span class="string">&#x27;Content-Type&#x27;</span>] == <span class="string">&quot;application/x-www-from-urlencoded&quot;</span>)&#123;</span><br><span class="line">            <span class="built_in">this</span>.bodyText = <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.body).map(<span class="function"><span class="params">key</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;key&#125;</span>=<span class="subst">$&#123;<span class="built_in">this</span>.body[key]&#125;</span>`</span>).join(<span class="string">&quot;&amp;&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//我们处理完那些传来的参数就把他们拼起来，拼成http听得懂的话，</span></span><br><span class="line">    <span class="comment">//注意空格不能多加 不然 http就听不懂了</span></span><br><span class="line">    <span class="function"><span class="title">toString</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.method&#125;</span> / HTTP/1.1\r</span></span><br><span class="line"><span class="string"><span class="subst">$&#123;<span class="built_in">Object</span>.keys(<span class="built_in">this</span>.headers).map(key =&gt; <span class="string">`<span class="subst">$&#123;key&#125;</span>:<span class="subst">$&#123;<span class="built_in">this</span>.headers[key]&#125;</span>`</span>).join(<span class="string">&quot;\r\n&quot;</span>)&#125;</span></span></span><br><span class="line"><span class="string">Content-Length:<span class="subst">$&#123;<span class="built_in">this</span>.bodyText.length&#125;</span>\r</span></span><br><span class="line"><span class="string">\r</span></span><br><span class="line"><span class="string"><span class="subst">$&#123;<span class="built_in">this</span>.bodyText&#125;</span>`</span>&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//用node的net模块发送我们拼好的语句</span></span><br><span class="line">    <span class="function"><span class="title">send</span>(<span class="params">connect</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(connect)&#123;</span><br><span class="line">            connect.write(<span class="built_in">this</span>.toString())</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">// let res = new ResponseParser();</span></span><br><span class="line">            connect = net.createConnection(&#123;</span><br><span class="line">                host:<span class="built_in">this</span>.host,</span><br><span class="line">                port:<span class="built_in">this</span>.port</span><br><span class="line">            &#125;,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">                connect.write(<span class="built_in">this</span>.toString());</span><br><span class="line">            &#125;)</span><br><span class="line">            connect.on(<span class="string">&quot;data&quot;</span>,<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">                    <span class="comment">// res.recive(data.toString())</span></span><br><span class="line">                    <span class="comment">// console.log(res.statusLine);</span></span><br><span class="line">                    connect.end();</span><br><span class="line">                &#125;)</span><br><span class="line">            connect.on(<span class="string">&quot;end&quot;</span>,<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">&quot;断开连接&quot;</span>);</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;发送成功&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>使用<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> net = <span class="built_in">require</span>(<span class="string">&#x27;net&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> req = <span class="keyword">new</span> Request(&#123;</span><br><span class="line">    method:<span class="string">&quot;POST&quot;</span>,</span><br><span class="line">    port:<span class="number">8080</span>,</span><br><span class="line">    body:&#123;</span><br><span class="line">        name:<span class="string">&quot;eww11&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    headers:&#123;</span><br><span class="line">     [<span class="string">&quot;Content-Type&quot;</span>]:<span class="string">&quot;application/x-www-from-urlencoded&quot;</span>,</span><br><span class="line">     [<span class="string">&quot;X-Foo&quot;</span>]:<span class="string">&quot;qqqs&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line"> &#125;);</span><br></pre></td></tr></table></figure></li>
<li>服务器的那端headers会打印什么<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &#x27;content-type&#x27;: &#x27;application/x-www-from-urlencoded&#x27;,</span><br><span class="line">  &#x27;x-foo&#x27;: &#x27;qqqs&#x27;,</span><br><span class="line">  &#x27;content-length&#x27;: &#x27;10&#x27;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>我们从服务器那边得到什么<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">HTTP/1.1 200 OK</span><br><span class="line">Content-Type: text/plain;charset=utf-8</span><br><span class="line">access-control-allow-origin: *</span><br><span class="line">Date: Wed, 27 Jan 2021 10:27:41 GMT</span><br><span class="line">Connection: keep-alive</span><br><span class="line">Transfer-Encoding: chunked</span><br><span class="line"></span><br><span class="line">3</span><br><span class="line">aaa</span><br><span class="line">0</span><br></pre></td></tr></table></figure></li>
</ul>
<p>2.处理http响应(处理我们得到的东西)</p>
<blockquote>
<p>我们需要写一个记录状态的状态机(其实就是把字一个个抽离处理,遇到\r或者\n，我们就怎么怎么样，遇到header头我们怎么去处理)</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ResponseParser</span></span>&#123;</span><br><span class="line">    <span class="comment">//字面意思，遇到不同的字符，都有一个数字对应</span></span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.WAITING_STATUS_LINE = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">this</span>.WAITING_STATUS_LINE_END = <span class="number">1</span>;</span><br><span class="line">        <span class="built_in">this</span>.WAITING_HEADER_NAME = <span class="number">2</span>;</span><br><span class="line">        <span class="built_in">this</span>.WAITING_HEADER_VALUE = <span class="number">3</span>;</span><br><span class="line">        <span class="built_in">this</span>.WAITING_HEADER_LINE_END = <span class="number">4</span>;</span><br><span class="line">        <span class="built_in">this</span>.WAITING_HEADER_BLOCK_END = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">this</span>.current = <span class="built_in">this</span>.WAITING_STATUS_LINE;</span><br><span class="line">        <span class="built_in">this</span>.statusLine = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="built_in">this</span>.headers = &#123;&#125;;</span><br><span class="line">        <span class="built_in">this</span>.headerName = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="built_in">this</span>.headerValue = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//把服务器发给我们的东西 一个个放入reciveChar()</span></span><br><span class="line">    <span class="function"><span class="title">recive</span>(<span class="params">string</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>;i &lt; string.length;i++)&#123;</span><br><span class="line">            <span class="built_in">this</span>.reciveChar(string.charAt(i));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//根据不同的字符 来做不同的处理</span></span><br><span class="line">    <span class="function"><span class="title">reciveChar</span>(<span class="params">char</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//这里只写了一个if，其实上有好多if else,这段if就是读取response的第一段,遇到\r\n停下</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">this</span>.current == <span class="built_in">this</span>.WAITING_STATUS_LINE)&#123;</span><br><span class="line">            <span class="keyword">if</span>(char == <span class="string">&#x27;\r&#x27;</span>)&#123;</span><br><span class="line">                <span class="built_in">this</span>.current = <span class="built_in">this</span>.WAITING_HEADER_LINE_END;</span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(char == <span class="string">&#x27;\n&#x27;</span>)&#123;</span><br><span class="line">                <span class="built_in">this</span>.current = <span class="built_in">this</span>.WAITING_HEADER_NAME</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="built_in">this</span>.statusLine += char;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>使用<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> res = <span class="keyword">new</span> ResponseParser();</span><br><span class="line">            connect = net.createConnection(&#123;</span><br><span class="line">                host:<span class="built_in">this</span>.host,</span><br><span class="line">                port:<span class="built_in">this</span>.port</span><br><span class="line">            &#125;,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">                connect.write(<span class="built_in">this</span>.toString());</span><br><span class="line">            &#125;)</span><br><span class="line">            connect.on(<span class="string">&quot;data&quot;</span>,<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">                    res.recive(data.toString())</span><br><span class="line">                    <span class="built_in">console</span>.log(res.statusLine); <span class="comment">//打印HTTP/1.1 200 OK</span></span><br><span class="line">                    connect.end();</span><br><span class="line">                &#125;)</span><br><span class="line">            connect.on(<span class="string">&quot;end&quot;</span>,<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">&quot;断开连接&quot;</span>);</span><br><span class="line">            &#125;)</span><br></pre></td></tr></table></figure></li>
<li>最终处理完成的结果<br><img src="https://ftp.bmp.ovh/imgs/2021/01/4946990e34cae97e.jpg" loading="lazy"></li>
</ul>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I'm so cute. Please give me money.</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://tang-webs.oss-cn-hangzhou.aliyuncs.com/tang-webs/202101012315352.jpg"><img loading="lazy" src="https://tang-webs.oss-cn-hangzhou.aliyuncs.com/tang-webs/202101012315352.jpg" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>唐奇杭</li><li class="post-copyright-link"><strong>Post link: </strong><a href="https://tangqihang.gitee.io/blog/2021/01/27/node/tcp%E6%A8%A1%E6%8B%9Fhttp%E8%AF%B7%E6%B1%82%E4%BB%A5%E5%8F%8A%E5%93%8D%E5%BA%94%E5%A4%84%E7%90%86/" title="tcp模拟http请求以及响应处理">https://tangqihang.gitee.io/blog/2021/01/27/node/tcp%E6%A8%A1%E6%8B%9Fhttp%E8%AF%B7%E6%B1%82%E4%BB%A5%E5%8F%8A%E5%93%8D%E5%BA%94%E5%A4%84%E7%90%86/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/blog/2021/04/08/go/gee%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1%E5%AD%A6%E4%B9%A0/" rel="prev" title="gee动态路由学习"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">gee动态路由学习</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/blog/2021/01/08/node/koa%E7%9A%84%E4%B8%80%E4%B8%AA%E5%9D%91-cookie/" rel="next" title="koa的一个坑--cookie"><span class="post-nav-text">koa的一个坑--cookie</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div id="comment"><div class="comment-tooltip text-center"><span>点击按钮跳转 GitHub Issues 评论。</span><br><span>若没有本文 Issue，您可以使用 Comment 模版新建。</span><br><a class="hty-button hty-button--raised" id="github-issues" target="_blank" rel="noopener" href="https://github.com/YunYouJun/yunyoujun.github.io/issues?q=is:issue+tcp模拟http请求以及响应处理">GitHub Issues</a></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 唐奇杭</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.3.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.3.0</span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="Search"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/blog/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="Searching..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/blog/js/utils.js"></script><script defer src="/blog/js/hexo-theme-yun.js"></script></body></html>